package net.flintx.array;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by flintx on 2018/1/5.
 */
public class PlusOne {
    public int[] plusOne(int[] digits) {
        /**
         * plusOne
         *
         * @Description:
         *  Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
         *  You may assume the integer do not contain any leading zero, except the number 0 itself.
         *  The digits are stored such that the most significant digit is at the head of the list.
         * @Example:
         * @Params: [digits]
         * @Return: int[]
         * @Date: 2018/1/5
         */
        int carry = 0;
        digits[digits.length - 1] += 1;
        for (int i = digits.length - 1; i >= 0; i--) {
            int temp = digits[i] + carry;
            digits[i] = temp % 10;
            carry = temp / 10;
        }
        if (carry == 1) {
            List<Integer> newDigits = new ArrayList<>();
            newDigits.add(1);
            for (int digit :
                    digits) {
                newDigits.add(digit);
            }
            Integer[] digitsInt = new Integer[newDigits.size()];
            Integer[] d = newDigits.toArray(digitsInt);
            digits = Arrays.stream(d).mapToInt(Integer::intValue).toArray();
        }
        return digits;
    }

    public static void main(String[] args) {
        int[] digits = {9, 9, 9, 9};
        System.out.println(Arrays.toString(new PlusOne().plusOne(digits)));
    }
}
